import { Context } from "koa";

const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const app = new Koa();
const router = require('./router/router');
const ws_router = require('./router/ws_router');

// 静态资源目录
const serve = require('koa-static');
app.use(serve(__dirname + '/public'))


// 连接数据库
const mongoose = require('mongoose');
const { dbUrl } = require('../config/config.ts');
mongoose.connect(dbUrl)
    .then(() => { console.log('Mongodb Connected..'); })
    .catch((err: Error) => { console.log(err); })

// 引入websocket模块 
const websockify = require('koa-websocket');
const ws_app = websockify(app);
ws_app.ws.use((ctx: Context, next: Function) => {
    ctx.websocket.send('连接成功');
    return next(ctx);
})

app.use(bodyParser());
app.use(router.routes()).use(router.allowedMethods());
ws_app.ws.use(ws_router.routes()).use(ws_router.allowedMethods());


const PORT = process.env.PORT || 3000;
ws_app.listen(PORT, () => {
    console.log('server started on port 3000');
});